﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StockAnalyze.aspx.cs" Inherits="Framework.StockAnalyze1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>3Wco.vn</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link href="/Css/top.css" rel="stylesheet" />
    <link href="/Css/stock.css" rel="stylesheet" />
    <link href="/Js/bootstrap-3.3.4/css/bootstrap.min.css" rel="stylesheet" />
    <link href="/Css/jquery.toast.css" rel="stylesheet" />
    <link href="/Css/contextMenu.css" rel="stylesheet" />
    <script src="/Js/jquery2.1.4.js"></script>
    <script type="text/javascript">
        var language = '<%= Session["language"]%>';
        if (language == '')
            language = 'vi-VN';
    </script>
    <script src="/Js/langvn.js"></script>
    <script src="/Js/jquery.toast.js"></script>
    <script src="/Js/bootstrap-3.3.4/js/bootstrap.min.js"></script>
    <script src="/Js/jssor.slider.mini.js"></script>
    <script src="/Js/jquery-ui-1.11.4.custom/jquery-ui-1.11.4.custom/jquery-ui.min.js"></script>
    <script src="/Js/jquery.validate.js"></script>
    <script src="/Js/base.js"></script>
    <script src="/Js/jsta.js?verson=100"></script>
    <script src="/Js/stats.js"></script>
    <script src="/Js/underscore-min.js"></script>
    <script src="/Js/drawMatch.js"></script>
    <script src="/Js/appcontextmenu.js"></script>

</head>
<body style="background: #000;">
    <input type="hidden" id="hdLanguage" runat="server" />

    <section id="section-content" style="background: #000; border-bottom: none;">
        <div class="container-fluid">
            <div class="row" style="margin-bottom: 0;">
                <div class="col-md-12">
                    <div class="row topbar" style="margin-bottom: 0; border-bottom: 1px solid #464545">
                        <div class="col-md-5 centertop">
                            <ul class="ulDetailStockTopCenter" style="padding: 0; margin: 0">
                                <li style="font-weight: bold"><%=DateTime.Now.ToString("dd/MM/yyyy") %> <span id="serverClock"><%=DateTime.Now.ToString("HH:mm:ss") %></span></li>
                                <li>VNI <span id="spVNI"></span></li>
                                <li>VN30 <span id="spVN30"></span></li>
                                <li>HNX <span id="spHNX"></span></li>
                                <li>HNX30 <span id="HNX30"></span></li>
                            </ul>
                        </div>
                        <div class="col-md-5 breadcrumb-member pull-right" style="padding: 0 2px">
                            <ul class="pull-right ulSetting">
                                <li>
                                    <form runat="server">
                                        <asp:ImageButton CssClass="img-language-vn" ID="ImageButton1" rel="vi-VN" ToolTip="Tiếng Việt" ImageUrl="/Images/vi.png" runat="server" OnClick="SetLanguage" />
                                        <asp:ImageButton CssClass="img-language-en" ID="ImageButton2" rel="en-US" ToolTip="English" ImageUrl="/Images/uk.png" runat="server" OnClick="SetLanguage" />
                                    </form>
                                </li>
                                <li style="margin-right: 10px"><%= DGSFramework.Globals.GetLanguage("Xin chào", "Welcome", true)%>, <strong>
                                    <asp:Label ID="lblMemberEmail" runat="server"></asp:Label>,<a href="Logout.aspx">
                                        <strong>
                                            <label style="color: #fff"><%= DGSFramework.Globals.GetLanguage("Thoát", "Log Out", true)%></label></strong></a></strong></li>
                            </ul>
                        </div>

                    </div>
                    <div class="row" style="margin-bottom: 0">
                        <div class="col-md-12 detailStock">

                            <div class="col-md-4 detailStockLeft" style="padding: 0">
                                <div class="col-md-12" style="padding-left: 0; padding-right: 0">
                                    <div class="dropdown dlStockType" id="dlStockType" style="display: inline-block">
                                        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            <%= DGSFramework.Globals.GetLanguage("Danh sách cổ phiếu", "ALL STOCK", true)%>
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu" aria-labelledby="dropdownMenu2" style="width: 120px">
                                            <asp:Literal runat="server" ID="ltDropdownMenu"></asp:Literal>
                                        </ul>

                                        <%--<ul class="dropdown-menu" aria-labelledby="dropdownMenu2" style="width: 120px">
                                            <li><a href="#" data-id="AllStock">ALL STOCK</a></li>
                                            <li><a href="#" data-id="TopVolume">TOP VOLUME</a></li>
                                            <li><a href="#" data-id="TopGainer">TOP GAINER</a></li>
                                            <li><a href="#" data-id="TopLoser">TOP LOSER</a></li>
                                            <li><a href="#" data-id="TopActive">TOP ACTIVE</a></li>
                                            <li><a href="#" data-id="myfavorite_5">MY FAVORITE 1</a></li>
                                            <li><a href="#" data-id="myfavorite_6">MY FAVORITE 2</a></li>
                                            <li><a href="#" data-id="myfavorite_7">MY FAVORITE 3</a></li>
                                            <li><a href="#" data-id="myfavorite_8">MY FAVORITE 4</a></li>
                                            <li><a href="#" data-id="myfavorite_9">MY FAVORITE 5</a>     </li>
                                            <li><a href="#" data-id="watchlistbuy">WATCHLIST - BUY</a></li>
                                            <li><a href="#" data-id="watchlistsell">WATCHLIST - SELL</a></li>
                                            <option value="RSI30">SCREEN STOCK 1</option>
                                            <option value="SO20">SCREEN STOCK 2</option>
                                        </ul>--%>
                                    </div>
                                    <input type="text" id="txtKeySearch" placeholder="<%= DGSFramework.Globals.GetLanguage("Tìm Cổ Phiếu", "Search Stock", true)%>" />
                                    <button id="btnSearch"><i class="glyphicon glyphicon-search"></i></button>
                                    <div class="pull-right" id="paging-wrap">
                                        <button style="padding: 0; width: 23px; height: 26px" id="btnFirstTblStock"><i class="glyphicon glyphicon-step-backward"></i></button>
                                        <button style="padding: 0; width: 23px; height: 26px" id="btnPrevblStock"><i class="glyphicon glyphicon-triangle-left"></i></button>
                                        <button style="padding: 0; width: 23px; height: 26px" id="btnNextTblStock"><i class="glyphicon glyphicon-triangle-right"></i></button>
                                        <button style="padding: 0; width: 23px; height: 26px" id="btnLastTblStock"><i class="glyphicon glyphicon-step-forward"></i></button>
                                    </div>
                                </div>
                                <div class="col-md-12 detailStockLeft" style="overflow-y: scroll; padding: 0; overflow-x: hidden;">
                                    <table class="table table-bordered table-hover" id="stockList">
                                        <thead>
                                            <tr>
                                                <td><%= DGSFramework.Globals.GetLanguage("CP", "Stock", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Đóng cửa", "Close", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Mở cửa", "Open", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Thấp nhất", "Low", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Cao nhất", "High", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Mua", "Buy", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Bán", "Sell", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Tổng mua", "BT", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Tổng bán", "ST", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Ngành", "Sector", true)%></td>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                </div>

                                <div class="col-md-12" style="padding: 0" id="tblTKTaiChinh">
                                </div>
                            </div>
                            <div class="col-md-6 detailStockCenter" style="padding: 0; width: 55.6%;">
                                <div class="col-md-12">
                                    <button id="chartsixmonth"><%= DGSFramework.Globals.GetLanguage("6T", "6M", true)%></button>
                                    <button id="chartoneyear" class="active"><%= DGSFramework.Globals.GetLanguage("1N", "1Y", true)%></button>
                                    <button id="chartfouryear"><%= DGSFramework.Globals.GetLanguage("4N", "4Y", true)%></button>
                                    <button id="checkBt" runat="server" visible="false">Check BT,ST</button>
                                    <label id="stockName"></label>
                                </div>
                                <div class="col-md-12 main-chart" style="padding: 0; margin: 0; height: 405px">
                                    <div style="display: none">
                                        <select id="selectts" style="margin: 0; padding: 0">
                                            <option value="0">Daily</option>
                                        </select>
                                        <select id="selectstyle">
                                            <option value="1">Dark</option>
                                            <option value="0">Light</option>
                                        </select>

                                        <input type="text" id="zoomwidth" size="12" style="display: none" />
                                        <select id="selectscale">
                                            <option value="0">Linear</option>
                                            <option value="1">Log</option>
                                        </select>
                                    </div>
                                    <div id="chart" style="height: 360px; position: relative">
                                    </div>
                                    <div style="position: absolute; opacity: 0.5; width: 0px; border-left-width: 1px; border-left-style: solid; z-index: 0; pointer-events: none;" id="lineTooltip"></div>
                                    <div style="position: absolute; opacity: 0.5; width: 0px; border-top-width: 1px; border-top-style: solid; z-index: 0; pointer-events: none;" id="lineTooltipX"></div>
                                </div>
                                <div id="container-match-wrap">
                                    <div class="col-md-12" id="tblQueueCurrent">
                                    </div>
                                    <div class="col-md-12" id="tblCurrentMatch">
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-2 detailStockRight">
                                <div class="tblRight">
                                    <table class="table table-bordered table-hover" id="tblCurrentPriceBoard">
                                        <thead>
                                            <tr>
                                                <td><%= DGSFramework.Globals.GetLanguage("Thời gian", "Time", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Giá", "Price", true)%></td>
                                                <td><%= DGSFramework.Globals.GetLanguage("Khối lượng", "Vol", true)%></td>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <div class="modal fade modalHistoryData">
        <div class="modal-dialog" style="width: 50%; float: right; margin: 0">
            <div class="modal-content">
                <div class="modal-header" style="padding: 0">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title"></h4>
                </div>
                <div class="modal-body" style="background: #000; overflow: hidden; padding: 0 0 10px 0">
                    <div style="width: 100%; color: #fff; font-size: 11px">
                    </div>
                    <%--<div class="left-modal" style="width: 50%; float: left"></div>
                    <div class="right-modal" style="width: 50%; float: left"></div>--%>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <!-- /.modal -->

    <asp:Panel runat="server" Visible="false" ID="pnCheckBt">
        <%-- dung de check bt,st--%>
        <div class="modal fade modalHistoryTable">
            <div class="modal-dialog" style="width: 50%; float: right; margin: 0">
                <div class="modal-content">
                    <div class="modal-header" style="padding: 0">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title"></h4>
                    </div>
                    <div class="modal-body" style="background: #000; overflow: hidden; padding: 0 0 10px 0">
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
    </asp:Panel>

    <div class="modal fade modalFavorite">
        <div class="modal-dialog" style="width: 50%; float: right; margin: 5px">
            <div class="modal-content">
                <div class="modal-header" style="padding: 5px">
                    <img src="Images/add.png" style="width: 32px; float: left" />
                    <h4><%= DGSFramework.Globals.GetLanguage("Thêm thư mục mới", "Create Favourite Folder", true)%></h4>
                </div>
                <div class="modal-body" style="background: #fff; overflow: hidden; padding: 10px">
                    <table>
                        <tr>
                            <td><%= DGSFramework.Globals.GetLanguage("Tên thư mục mới", "Favorite Name", true)%></td>
                            <td>
                                <input type="text" id="FavoriteName" style="margin-left: 5px; padding: 5px;" />
                            </td>
                            <td>
                                <button style="height: 33px; border-radius: 0; margin-left: 2px;" type="button" class="btn btn-sm btn-primary" id="btnAddFav"><%= DGSFramework.Globals.GetLanguage("Thêm mới", "Create", true)%></button>
                            </td>
                        </tr>
                    </table>
                    <hr />
                    <table id="favList" class="table">
                        <thead>
                            <tr>
                                <th><%= DGSFramework.Globals.GetLanguage("Tên thư mục mới", "Favourite Name", true)%></th>
                                <th>#</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <button style="border-radius: 0;" type="button" class="btn btn-default" data-dismiss="modal"><%= DGSFramework.Globals.GetLanguage("ĐÓNG", "Close", true)%></button>
                </div>
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>

    <asp:Literal runat="server" ID="ltMenuContext"></asp:Literal>
</body>

<script type="text/javascript">
    var serverClock = jQuery("#serverClock");
    var maCP = 'AAA';
    var ticker;
    var dataStock;
    var dataTemp;
    var flag = 0;
    var myVar, myVar1;
    
    $(document).ready(function () {    

        //set oclock
        if (serverClock.length > 0) {
            showServerTime(serverClock, serverClock.text());
        }

        $('div#dlStockType').on('click', 'ul li a', function () {
            $item = $(this);
            typeStock = $item.data('id');
            if (typeStock == "newfolder") {
                OpenAddFavorite();
                return false;
            }
            $('div#dlStockType button').html($item.text() + ' <span class="caret"></span>');
            pagingCurrrentPage = 1;
            LoadStock(false, false);
            RefreshContext();
        })

        $(".modalHistoryData .modal-dialog").draggable({
            handle: ".modal-header"
        });
        GetHoseIndex();
        SetColorStockList();

        $(".btnAddFavoMN").click(function () {
            $(".rAddFavourite").show(400);
        })
        $("#btnCloseFavourite").click(function () {
            $(".rAddFavourite").hide(400);
        });
        var isstockListProcessing = false;
        $('#stockList tbody').on('click', 'td', function (e) {
            if (isstockListProcessing == false) {
                isstockListProcessing = true;
                var $parent = $(this).parent();
                $('#stockList td').prop('disabled', true);
                //reset popup position
                $('.modal-dialog').css('top', 0).css('left', 0);
                Loading();
                $('#stockList tbody tr').removeClass('active');

                $parent.addClass('active');
                var currentCode = $parent.attr('tcode');
                var currentDate = $parent.attr('tdate');
                LoadPriceBoard(currentCode, currentDate);
                DrawTableTaiChinh(currentCode);
                isstockListProcessing = false;
            }
        })

        $('#paging-wrap').on('click', 'button', function () {
            var currentID = $(this).attr('id');
            if (currentID == 'btnNextTblStock') {
                if (pagingCurrrentPage != pagingTotalPage) {
                    pagingCurrrentPage += 1;
                    LoadStock(false, false);
                }
            }
            else if (currentID == 'btnLastTblStock') {
                if (pagingCurrrentPage != pagingTotalPage) {
                    pagingCurrrentPage = pagingTotalPage;
                    LoadStock(false, false);
                }
            }
            else if (currentID == 'btnFirstTblStock') {
                if (pagingCurrrentPage != 1) {
                    pagingCurrrentPage = 1;
                    LoadStock(false, false);
                }
            }
            else if (currentID == 'btnPrevblStock') {
                if (pagingCurrrentPage != 1) {
                    pagingCurrrentPage -= 1;
                    LoadStock(false, false);
                }
            }
            return false;
        })

        $('#tblQueueCurrent').on('click', 'button', function () {
            $(".modalHistoryData").modal();
            return false;
        })
        $('#tblCurrentMatch').on('click', 'button', function () {
            $(".modalHistoryData").modal();
            return false;
        })
        $('#<%=checkBt.ClientID%>').click(function () {
            $(".modalHistoryTable").modal();
            return false;
        })
        $('#btnUpdateList').click(function () {
            var type = $('#dlStockType').val();
            if (type == 'watchlistbuy' || type == 'watchlistsell') {
                pagingCurrrentPage = 1;
                LoadStock(false, false);
            }
            return false;
        })

        $('#btnSearch').click(function () {
            pagingCurrrentPage = 1;
            var code = $('#txtKeySearch').val().trim();
            if (code.length > 0) {
                LoadStock(false, true);
            }
            return false;
        })

        $('#txtKeySearch').on('keypress', function (event) {
            if (event.which === 13) {
                var code = $('#txtKeySearch').val().trim();
                if (code.length > 0)
                    LoadStock(false, true);
            }
        });
        LoadStock(true, false);
        $("#chartsixmonth").click(function (event) {
            $(this).addClass('active');
            $("#chartoneyear").removeClass('active');
            $("#chartfouryear").removeClass('active');
            var p = window.$plot;
            if (!p) return false;
            p.zoomtype(2, dataStock);
        });
        $("#chartoneyear").click(function (event) {
            $(this).addClass('active');
            $("#chartsixmonth").removeClass('active');
            $("#chartfouryear").removeClass('active');
            var p = window.$plot;
            if (!p) return false;
            p.zoomtype(3, dataStock);
        });
        $("#chartfouryear").click(function (event) {
            $(this).addClass('active');
            $("#chartsixmonth").removeClass('active');
            $("#chartoneyear").removeClass('active');
            var p = window.$plot;
            if (!p) return false;
            p.zoomtype(4, dataStock);
        });
    })
    function LoadPriceBoard(ticket, currentDate) {        
        LoadChart(ticket, currentDate);        
        $('#stockName').text(ticket);
        //clear priceboard
        $('#tblCurrentPriceBoard tbody').html('');
        //clear table current match
        $('#tblCurrentMatch').html('');
        //clear table current queue
        $('#tblQueueCurrent').html('');
        //clear table tai chinh
        $('#tblTKTaiChinh').html('');
        //clear popup history data
        $('.modalHistoryData .modal-body .right-modal').html('');
        $('.modalHistoryData .modal-body .left-modal').html('');
        $.ajax({
            url: "/Resources/GetStockDataDetails.ashx",
            method: "POST",
            data: { stock: ticket, getTime: currentDate },
            dataType: "json",
            success: function (response) {
                var html = '';
                var htmlBuy = '';
                var htmlSell = '';
                var dataBuy = [];
                var dataSell = [];
                var totalBuy = 0;
                var totalSell = 0;
                var maxBuy = 0;
                var maxSell = 0;
                if (response.Status == 1) {
                    var data = response.Data;
                    var dataLength = data.length;
                    if (dataLength > 0) {
                        DrawTable(data, response.Code);
                    }
                }
                
            },
            error: function (xhr, ajaxOptions, thrownError) {
                $('#stockList td').prop('disabled', false);
                ClearLoading();
                alert(thrownError);
            }
        }).done(function () {
            $('#stockList td').prop('disabled', false);
        })

    }

    function DrawTable(data, code) {
        var dataLength = data.length;
        if (dataLength > 0) {
            for (var i = 0; i < dataLength; i++) {
                if (i == 0) {
                    DrawQueueCurrent(data[i]);
                    DrawTransactionBoard(data[i]);

                }
            }
            //DrawTableTaiChinh(data);
            DrawHistoryData(data, code);
            DrawTableCheck(data);
        }
    }

    function DrawTransactionBoard(data) {
        if (data != null) {
            var tblCurrentPriceBoardHtml = '';
            var arrayValue = data.ArrayValue.split('^');
            var arrayQueue = [];
            var arrayQueueKL_Gia = [];
            var isFirst = true;
            var preDM = '', preDB = ''; //luu gia tri truoc do cua gia, de tinh cho truong hop DM_Gia1 hoac DB_Gia1 khong co
            for (var j = 0; j < arrayValue.length; j++) {
                var dataArray = null;
                if (arrayValue[j] != null)
                    dataArray = arrayValue[j].split('|');
                if (dataArray.length > 10) {

                    var DM_Gia3 = dataArray[2], DM_Gia2 = dataArray[4], DM_Gia1 = dataArray[6], KL_Gia = dataArray[8], KL_KL = dataArray[9];
                    var DB_Gia1 = dataArray[11], DB_Gia2 = dataArray[13], DB_Gia3 = dataArray[15];
                    var date = (dataArray[0].split(' '))[1];
                    var DB = '', DM = '';
                    DB = DB_Gia1;
                    DM = DM_Gia1;


                    if ((DB == 'ATC' || DB == 'ATO' || DM == 'ATC' || DM == 'ATO') || (KL_Gia != '' && KL_KL != '' && KL_KL.indexOf('+006') < 0 && DB != 'ATC' && DB != 'ATO' && DM != 'ATC' && DM != 'ATO')) {
                        var date = (dataArray[0].split(' '))[1];
                        if (DB == 'ATC' || DB == 'ATO' || DM == 'ATC' || DM == 'ATO') { //ato, atc chi hien thi chu ko tinh bt,st
                            tblCurrentPriceBoardHtml += '<tr><td>' + date + '</td><td>' + DinhDang(parseFloat(KL_Gia)) + '</td><td class="color_purple">' + DinhDang(KL_KL) + '</td></tr>';
                        }
                        else {
                            if (isFirst == false) {
                                if (DM == '')
                                    DM = preDM;
                                if (DB == '')
                                    DB = preDB;
                            }
                            preDM = DM;
                            preDB = DB;
                            isFirst = false;
                            var color = 'color_red';
                            if (KL_Gia == DB || KL_Gia > DM)
                                color = 'color_green';
                            KL_Gia = parseFloat(KL_Gia);
                            DM = parseFloat(DM);
                            DB = parseFloat(DB);
                            //tblCurrentPriceBoardHtml += '<tr><td>' + date + '</td><td>' + DinhDang(KL_Gia) + '</td><td class="' + color + '">' + DinhDang(KL_KL) + '</td></tr>';
                            $('#tblCurrentPriceBoard tbody').append('<tr><td>' + date + '</td><td>' + DinhDang(KL_Gia) + '</td><td class="' + color + '">' + DinhDang(KL_KL) + '</td></tr>');
                            KL_Gia = parseFloat(KL_Gia);
                            arrayQueue.push([KL_Gia, DM, DB, KL_KL]);
                            if ($.inArray(KL_Gia, arrayQueueKL_Gia) < 0 && KL_Gia > 0)
                                arrayQueueKL_Gia.push(KL_Gia);
                        }

                    }
                }
            }
            //            $('#tblCurrentPriceBoard tbody').html(tblCurrentPriceBoardHtml);
            //xu ly table match

            var arrayMatchList = [];
            var maxVolume = 0;
            var arrayGroupSort = arrayQueueKL_Gia.sort(function (a, b) { return b - a });
            arrayGroupSort.forEach(function (arrayGroupItem) {
                var buyCount = 0, sellCount = 0, buyVolume = 0, sellVolume = 0;
                arrayQueue.forEach(function (item) {
                    if (item[0] == arrayGroupItem) {
                        var KL_Gia = item[0], DM = item[1], DB = item[2];
                        if (KL_Gia == DB || KL_Gia > DM) //nhathn 06/11/2015
                        {
                            buyCount += 1;
                            buyVolume += parseInt(item[3]);
                        }
                        else {
                            sellCount += 1;
                            sellVolume += parseInt(item[3]);
                        }
                    }
                })
                maxVolume = buyVolume > maxVolume ? buyVolume : maxVolume;
                maxVolume = sellVolume > maxVolume ? sellVolume : maxVolume;
                arrayMatchList.push([buyCount, buyVolume, parseFloat(arrayGroupItem), sellVolume, sellCount]);
            });

            DrawMatchedCurrent(arrayMatchList, maxVolume, data.QueueDetails.GiaThamChieu);
            //end xu ly table match
        }
    }

    function DrawMatchedCurrent(dataMatched, maxMatchedVolume, giaThamChieu) {
        var html = '';
        var htmlMatched = '';
        var totalBuy = 0, totalSell = 0, totalSVolume = 0, totalBVolume = 0;
        var htmlMatchedTitle = '<div class="col-md-12 titletheadc" style="text-align: center;position:relative"><button id="btnHistoryData"><%= DGSFramework.Globals.GetLanguage("Dữ liệu 3 ngày", "Past 3 days data", true)%></button><p><%= DGSFramework.Globals.GetLanguage("Khớp lệnh Mua/Bán", "Matched Buy &amp; Sell", true)%>: CurrentMatchedTTVolume</p></div>';
        var htmlMatchedHeader = '<div class="col-md-12"><div class="col-md-6"><div class="r-stock"><div class="pull-left stock-item color_yellow">BT</div>';
        htmlMatchedHeader += '<div class="progress progress-stock pull-left">';
        htmlMatchedHeader += '<p style="float: left" class="color_yellow">TotalBuyCurrentMatched</p></div>';
        htmlMatchedHeader += '<div class="pull-left stock-item stock-item-left color_yellow" >TotalBuyCurrentVolumeMatched</div>';
        htmlMatchedHeader += '<div class="pull-left stock-item"><%= DGSFramework.Globals.GetLanguage("Mua", "Buy", true)%></div></div></div>';
        htmlMatchedHeader += '<div class="col-md-6"><div class="r-stock">';
        htmlMatchedHeader += '<div class="pull-right stock-item color_yellow">ST</div>';
        htmlMatchedHeader += '<div class="progress progress-stock pull-right">';
        htmlMatchedHeader += '<p style="float: right" class="color_yellow">TotalSellCurrentMatched</p></div>';
        htmlMatchedHeader += '<div class="pull-left stock-item"><%= DGSFramework.Globals.GetLanguage("Bán", "Sell", true)%></div>';
        htmlMatchedHeader += '<div class="pull-left stock-item stock-item-right color_yellow">TotalSellCurrentVolumeMatched</div>';
        htmlMatchedHeader += '</div></div></div>';

        for (var i = 0; i < dataMatched.length; i++) {
            var SCount = dataMatched[i][4];
            var BCount = dataMatched[i][0];
            var SVolume = dataMatched[i][3];
            var BVolume = dataMatched[i][1];
            var SPrice = dataMatched[i][2];
            var BPrice = dataMatched[i][2];
            totalSell += SCount;
            totalBuy += BCount;
            totalSVolume += SVolume;
            totalBVolume += BVolume;
            htmlMatched += '<div class="col-md-12">';
            htmlMatched += '<div class="col-md-6"><div class="r-stock">';
            htmlMatched += '<div class="pull-left stock-item">' + CheckNumberValue(BCount) + '</div>';
            htmlMatched += GetProgressingBar(BVolume, maxMatchedVolume, false);
            htmlMatched += '<div class="pull-left stock-item stock-item-left">' + DinhDang(BVolume) + '</div>';
            htmlMatched += '<div class="pull-left stock-item ' + SetColor(BPrice, giaThamChieu) + '">' + (CheckNumberValue(BPrice) == 0 ? "&nbsp" : CheckNumberValue(BPrice)) + '</div>';
            htmlMatched += '</div></div>';
            htmlMatched += '<div class="col-md-6"><div class="r-stock">';
            htmlMatched += '<div class="pull-right stock-item">' + CheckNumberValue(SCount) + '</div>';
            htmlMatched += GetProgressingBar(SVolume, maxMatchedVolume, true);
            htmlMatched += '<div class="pull-left stock-item ' + SetColor(SPrice, giaThamChieu) + '">' + (CheckNumberValue(SPrice) == 0 ? "&nbsp" : CheckNumberValue(SPrice)) + '</div>';
            htmlMatched += '<div class="pull-left stock-item stock-item-right">' + DinhDang(SVolume) + '</div>';
            htmlMatched += '</div></div></div>';
        }
        html += htmlMatchedTitle.replace('CurrentMatchedTTVolume', DinhDang(totalBVolume + totalSVolume));
        html += htmlMatchedHeader.replace('TotalBuyCurrentMatched', DinhDang(totalBuy)).replace('TotalSellCurrentMatched', DinhDang(totalSell)).replace('TotalBuyCurrentVolumeMatched', DinhDang(totalBVolume)).replace('TotalSellCurrentVolumeMatched', DinhDang(totalSVolume));
        html += htmlMatched;
        $('#tblCurrentMatch').html(html);
    }

    function DrawQueueCurrent(data) {
        var MaxQueue = data.MaxQueue;
        data = data.QueueDetails;
        var DM_KL1 = parseInt(data.DM_KL1);
        var DM_KL2 = parseInt(data.DM_KL2);
        var DM_KL3 = parseInt(data.DM_KL3);
        var DM_Gia1 = parseFloat(data.DM_Gia1);
        DM_Gia1 = CheckPrice(DM_Gia1);
        var DM_Gia2 = parseFloat(data.DM_Gia2);
        DM_Gia2 = CheckPrice(DM_Gia2);
        var DM_Gia3 = parseFloat(data.DM_Gia3);
        DM_Gia3 = CheckPrice(DM_Gia3);
        var DB_KL1 = parseInt(data.DB_KL1);
        var DB_KL2 = parseInt(data.DB_KL2);
        var DB_KL3 = parseInt(data.DB_KL3);
        var DB_Gia1 = parseFloat(data.DB_Gia1);
        DB_Gia1 = CheckPrice(DB_Gia1);
        var DB_Gia2 = parseFloat(data.DB_Gia2);
        DB_Gia2 = CheckPrice(DB_Gia2);
        var DB_Gia3 = parseFloat(data.DB_Gia3);
        DB_Gia3 = CheckPrice(DB_Gia3);
        var GiaThamChieu = parseFloat(data.GiaThamChieu);
        var totalBuyVolume = 0;
        totalBuyVolume += DM_KL1.toString() == "NaN" ? 0 : DM_KL1;
        totalBuyVolume += DM_KL2.toString() == "NaN" ? 0 : DM_KL2;
        totalBuyVolume += DM_KL3.toString() == "NaN" ? 0 : DM_KL3;
        var totalSellVolume = 0;
        totalSellVolume += DB_KL1.toString() == "NaN" ? 0 : DB_KL1;
        totalSellVolume += DB_KL2.toString() == "NaN" ? 0 : DB_KL2;
        totalSellVolume += DB_KL3.toString() == "NaN" ? 0 : DB_KL3;

        var html = '';
        html += ' <div class="col-md-12 titletheadc" style="position:relative"><button id="btnHistoryData"><%= DGSFramework.Globals.GetLanguage("Dữ liệu 3 ngày", "Past 3 days data", true)%></button>';
        html += '<p><%= DGSFramework.Globals.GetLanguage("Xếp hàng Mua/Bán", "Queue Buy &amp; Sell", true)%></p></div>';
        html += '<div class="col-md-5 s-top" style="margin-left: 30px;"><div class="r-stock">';
        html += '<div class="progress progress-stock pull-left"></div>';
        html += '<div class="pull-left stock-item stock-item-left color_yellow">' + DinhDang(totalBuyVolume) + '</div>';
        html += '<div class="pull-left stock-item"><%= DGSFramework.Globals.GetLanguage("Mua", "Buy", true)%></div>';
        html += '</div><div class="r-stock">';
        html += GetProgressingBar(DM_KL1, MaxQueue, false);
        html += '<div class="pull-left stock-item stock-item-left">' + DinhDang(DM_KL1) + '</div>';
        html += '<div class="pull-left stock-item ' + SetColor(DM_Gia1, GiaThamChieu) + '">' + DM_Gia1 + '</div>';
        html += '</div><div class="r-stock">';
        html += GetProgressingBar(DM_KL2, MaxQueue, false);
        html += '<div class="pull-left stock-item stock-item-left">' + DinhDang(DM_KL2) + '</div>';
        html += '<div class="pull-left stock-item ' + SetColor(DM_Gia2, GiaThamChieu) + '">' + DM_Gia2 + '</div></div><div class="r-stock">';
        html += GetProgressingBar(DM_KL3, MaxQueue, false);
        html += '<div class="pull-left stock-item stock-item-left">' + DinhDang(DM_KL3) + '</div>';
        html += '<div class="pull-left stock-item ' + SetColor(DM_Gia3, GiaThamChieu) + '">' + DM_Gia3 + '</div></div></div>';
        html += '<div class="col-md-5 s-top"><div class="r-stock"><div class="progress progress-stock pull-right"></div>';
        html += '<div class="pull-left stock-item"><%= DGSFramework.Globals.GetLanguage("Bán", "Sell", true)%></div>';
        html += '<div class="pull-left stock-item stock-item-right color_yellow">' + DinhDang(totalSellVolume) + '</div></div>';
        html += '<div class="r-stock">';
        html += GetProgressingBar(DB_KL1, MaxQueue, true);
        html += '<div class="pull-left stock-item ' + SetColor(DB_Gia1, GiaThamChieu) + '">' + DB_Gia1 + '</div>';
        html += '<div class="pull-left stock-item stock-item-right">' + DinhDang(DB_KL1) + '</div></div><div class="r-stock">';
        html += GetProgressingBar(DB_KL2, MaxQueue, true);
        html += '<div class="pull-left stock-item ' + SetColor(DB_Gia2, GiaThamChieu) + '">' + DB_Gia2 + '</div>';
        html += '<div class="pull-left stock-item stock-item-right">' + DinhDang(DB_KL2) + '</div></div><div class="r-stock">';
        html += GetProgressingBar(DB_KL3, MaxQueue, true);
        html += '<div class="pull-left stock-item ' + SetColor(DB_Gia3, GiaThamChieu) + '">' + DB_Gia3 + '</div>';
        html += '<div class="pull-left stock-item stock-item-right">' + DinhDang(DB_KL3) + '</div></div></div>';
        $('#tblQueueCurrent').html(html);
    }

    function GetProgressingBar(current, max, isRight) {
        var html = '';
        current = current != null ? parseInt(current) : 0;
        max = max != null ? parseInt(max) : 0;
        var progress = 0;
        if (max != 0)
            progress = current / max;
        progress = progress * 100;
        progress = progress > 0 && progress <= 1 ? 1 : progress;
        html += '<div class="progress progress-stock' + (isRight == true ? " pull-right" : " pull-left") + '">';
        html += '<div class="progress-bar progress-bar-warning" role="progressbar" style="width: ' + progress + '%; float: ' + (isRight == true ? " left" : " right") + '"></div></div>';
        return html;
    }

    function CheckNumberValue(value) {
        if (value == null || value.length == 0)
            return 0;
        else
            return value;
    }

    function CheckNull(value) {
        if (value == null || value.length == 0)
            return "";
        else
            return value;
    }

    function LoadStock(isStart, isSearch) {
        //clear stocklist
        $('#stockList tbody').html('');
        var code = '';
        if (isSearch)
            code = $('#txtKeySearch').val().trim();
        $.ajax({
            url: "/Resources/GetStockList.ashx",
            method: "POST",
            data: { type: typeStock, page: pagingCurrrentPage, code: code },
            dataType: "json",
            success: function (response) {
                if (response.Status == 1) {
                    var data = response.Data;
                    var html = '';
                    if (isStart) {
                        var codeStart = data[0].Code;
                        LoadPriceBoard(codeStart, data[0].GetTimeCondition);
                        DrawTableTaiChinh(codeStart);
                    }
                    for (var i = 0; i < data.length; i++) {
                        var checkDataCode = CheckNull(data[i].Code);
                        var checkDataKL_Gia = CheckNull(data[i].KL_Gia);
                        var checkDataThap = CheckNull(data[i].Thap);
                        var checkDataCao = CheckNull(data[i].Cao);
                        var checkDataDM_Gia1 = CheckNull(data[i].DM_Gia1);
                        var checkDataDB_Gia1 = CheckNull(data[i].DB_Gia1);
                        var checkDataGiaMoCua = CheckNull(data[i].GiaMoCua);
                        var checkDataGiaBT = CheckNull(data[i].BT);
                        var checkDataGiaST = CheckNull(data[i].ST);
                        var stockName = CheckNull(data[i].Name);
                        var sector = CheckNull(data[i].Sector);
                        if (language == 'en-US') {
                            stockName = CheckNull(data[i].Name_en);
                            sector = CheckNull(data[i].Sector_en);
                        }
                        var checkGiaThamChieu = CheckNull(data[i].GiaThamChieu);
                        if (checkDataDM_Gia1 == "-2")
                            //checkDataDM_Gia1 = "ATC";
                            checkDataDM_Gia1 = "-";
                        else if (checkDataDM_Gia1 == "-1")
                            //checkDataDM_Gia1 = "ATO";
                            checkDataDM_Gia1 = "-";
                        else if (checkDataDM_Gia1 == "")
                            checkDataDM_Gia1 = "-";
                        if (checkDataDB_Gia1 == "-2")
                            //checkDataDB_Gia1 = "ATC";
                            checkDataDB_Gia1 = "-";
                        else if (checkDataDB_Gia1 == "-1")
                            //checkDataDB_Gia1 = "ATO";
                            checkDataDB_Gia1 = "-";
                        else if (checkDataDB_Gia1 == "")
                            checkDataDB_Gia1 = "-";
                        html += '<tr tdate="' + data[i].GetTimeCondition + '" tcode="' + data[i].Code + '" title="' + stockName + '">';
                        html += '<td>' + checkDataCode + '</td>';
                        html += '<td>' + (checkGiaThamChieu != "" ? checkGiaThamChieu : "-") + '</td>';
                        html += '<td>' + (checkDataGiaMoCua != "" ? checkDataGiaMoCua : "-") + '</td>';
                        html += '<td>' + (checkDataThap != "" ? checkDataThap : "-") + '</td>';
                        html += '<td>' + (checkDataCao != "" ? checkDataCao : "-") + '</td>';
                        html += '<td>' + checkDataDM_Gia1 + '</td>';
                        html += '<td>' + checkDataDB_Gia1 + '</td>';
                        html += '<td>' + (checkDataGiaBT != "" ? checkDataGiaBT : "0") + '</td>';
                        html += '<td>' + (checkDataGiaST != "" ? checkDataGiaST : "0") + '</td>';
                        if (typeStock == 'watchlistbuy') {
                            var DM_Gia1 = CheckNull(data[i].DM_Gia1);
                            var DM_Gia2 = CheckNull(data[i].DM_Gia2);
                            html += '<td class="' + SetColor(data[i].DM_Gia1, data[i].GiaThamChieu) + '">' + (DM_Gia1 != "" ? DM_Gia1 : "-") + '</td>';
                            html += '<td class="' + SetColor(data[i].DM_Gia2, data[i].GiaThamChieu) + '">' + (DM_Gia2 != "" ? DM_Gia2 : "-") + '</td>';
                        }
                        else if (typeStock == 'watchlistsell') {
                            var DB_Gia1 = CheckNull(data[i].DB_Gia1);
                            var DB_Gia2 = CheckNull(data[i].DB_Gia2);
                            html += '<td class="' + SetColor(data[i].DB_Gia1, data[i].GiaThamChieu) + '">' + (DB_Gia1 != "" ? DB_Gia1 : "-") + '</td>';
                            html += '<td class="' + SetColor(data[i].DB_Gia2, data[i].GiaThamChieu) + '">' + (DB_Gia2 != "" ? DB_Gia2 : "-") + '</td>';
                        }
                        else
                            html += '<td style="text-align:left;padding-left:2px !important">' + sector + '</td>';
                        html += '</tr>';
                    }
                    pagingTotalPage = Math.ceil(parseInt(response.TotalRecord) / 50);
                    var headerTemplate = '<td>' + lblStock + '</td>' + '<td>' + lblClose + '</td>' + '<td>' + lblOpen + '</td>' + '<td>' + lblLow + '</td>' + '<td>' + lblHigh + '<td>' + lblBuy + '</td>' + '<td>' + lblSell + '</td>' + '</td>' + '<td>' + lblBT + '</td>' + '<td>' + lblST + '</td>' + '<td>' + lblSector + '</td>';
                    if (typeStock == 'watchlistbuy')
                        headerTemplate = '<td>' + lblStock + '</td>' + '<td>' + lblClose + '</td>' + '<td>' + lblOpen + '</td>' + '<td>' + lblLow + '</td>' + '<td>' + lblHigh + '<td>' + lblBuy + '</td>' + '<td>' + lblSell + '</td>' + '</td>' + '<td>' + lblBT + '</td>' + '<td>' + lblST + '</td>' + '<td>' + lblBuy1 + '</td>' + '<td>' + lblBuy2 + '</td>';
                    else if (typeStock == 'watchlistsell')
                        headerTemplate = '<td>' + lblStock + '</td>' + '<td>' + lblClose + '</td>' + '<td>' + lblOpen + '</td>' + '<td>' + lblLow + '</td>' + '<td>' + lblHigh + '</td>' + '<td>' + lblBuy + '</td>' + '<td>' + lblSell + '</td>' + '<td>' + lblBT + '</td>' + '<td>' + lblST + '</td>' + '<td>' + lblSell1 + '</td>' + '<td>' + lblSell2 + '</td>';
                    $('#stockList thead tr').html(headerTemplate);
                    $('#stockList tbody').html(html);
                    SetColorStockList();
                    LoadContext();
                }
            }
        })
    }

    function DrawHistoryData(data, code) {
        var htmlTemplate = '';
        var html = '';
        $('.modal-title').text(code + " - <%= DGSFramework.Globals.GetLanguage("Dữ Liệu 3 Ngày", "Past 3 Days Data", true)%>");
        var htmlMatchedRowTemplate = '<div class="col-md-12"><div class="col-md-6"><div class="r-stock"><div class="pull-left stock-item">BCount</div><div class="progress progress-stock pull-left"><div class="progress-bar progress-bar-warning" role="progressbar" style="width: BPercent%; float: right"></div></div><div class="pull-left stock-item stock-item-left">BVolume</div><div class="pull-left stock-item classBuyPrice">BPrice</div></div></div><div class="col-md-6"><div class="r-stock"><div class="pull-right stock-item">SCount</div><div class="progress progress-stock pull-right"><div class="progress-bar progress-bar-warning" role="progressbar" style="width: SPercent%; float:  left"></div></div><div class="pull-left stock-item classSellPrice">SPrice</div><div class="pull-left stock-item stock-item-right">SVolume</div></div></div></div>';
        htmlTemplate = '<div><h6 style="color: #fff;margin:5px;float:left;width:100%">DateData</h4><div class="col-md-12" style="padding: 0 2px; "><div class="col-md-12 titletheadc"><p><%= DGSFramework.Globals.GetLanguage("Xếp hàng Mua/Bán", "Queue Buy &amp; Sell", true)%></p></div><div class="col-md-5 s-top"><div class="r-stock"><div class="progress progress-stock pull-left"></div><div class="pull-left stock-item stock-item-left">TotalBuyVolume</div><div class="pull-left stock-item"><%= DGSFramework.Globals.GetLanguage("Mua", "Buy", true)%></div></div><div class="r-stock"><div class="progress progress-stock pull-left"><div class="progress-bar progress-bar-warning" role="progressbar" style="width: DM_percent3%; float: right; "></div></div><div class="pull-left stock-item stock-item-left">DM_KL3</div><div class="pull-left stock-item classDMGia3">DM_Gia3</div></div>';
        htmlTemplate += '<div class="r-stock"><div class="progress progress-stock pull-left"><div class="progress-bar progress-bar-warning" role="progressbar" style="width: DM_percent2%; float: right"></div>';
        htmlTemplate += '</div><div class="pull-left stock-item stock-item-left">DM_KL2</div><div class="pull-left stock-item classDMGia2">DM_Gia2</div></div><div class="r-stock">';
        htmlTemplate += '<div class="progress progress-stock pull-left"><div class="progress-bar progress-bar-warning" role="progressbar" style="width: DM_percent1%; float: right">';
        htmlTemplate += '</div></div><div class="pull-left stock-item stock-item-left">DM_KL1</div><div class="pull-left stock-item classDMGia1">DM_Gia1</div></div>';
        htmlTemplate += '</div><div class="col-md-5 s-top"><div class="r-stock"><div class="progress progress-stock pull-right"></div><div class="pull-left stock-item"><%= DGSFramework.Globals.GetLanguage("Bán", "Sell", true)%></div>';
        htmlTemplate += '<div class="pull-left stock-item stock-item-right">ToTalSellVolume</div></div><div class="r-stock">';
        htmlTemplate += '<div class="progress progress-stock pull-right"><div class="progress-bar progress-bar-warning" role="progressbar" style="width: DB_percent1%; float: left">';
        htmlTemplate += '</div></div><div class="pull-left stock-item classDBGia1">DB_Gia1</div><div class="pull-left stock-item stock-item-right">DB_KL1</div>';
        htmlTemplate += '</div><div class="r-stock"><div class="progress progress-stock pull-right"><div class="progress-bar progress-bar-warning" role="progressbar" style="width: DB_percent2%; float: left">';
        htmlTemplate += '</div></div><div class="pull-left stock-item classDBGia2">DB_Gia2</div><div class="pull-left stock-item stock-item-right">DB_KL2</div></div>';
        htmlTemplate += '<div class="r-stock"><div class="progress progress-stock pull-right"><div class="progress-bar progress-bar-warning" role="progressbar" style="width: DB_percent3%; float: left">';
        htmlTemplate += '</div></div><div class="pull-left stock-item classDBGia3">DB_Gia3</div><div class="pull-left stock-item stock-item-right">DB_KL3</div></div></div></div>';
        for (var i = 0; i < data.length; i++) {
            var MaxQueue = parseInt(data[i].MaxQueue);
            var DataDate = data[i].GetTimeStr;
            var dataQueueDetails = data[i].QueueDetails;
            var GiaThamChieu = dataQueueDetails.GiaThamChieu;
            var DM_KL1 = parseInt(dataQueueDetails.DM_KL1);
            var DM_KL2 = parseInt(dataQueueDetails.DM_KL2);
            var DM_KL3 = parseInt(dataQueueDetails.DM_KL3);
            var DM_Gia1 = parseFloat(dataQueueDetails.DM_Gia1);
            DM_Gia1 = DM_Gia1.toString() == 'NaN' ? '-' : DM_Gia1;
            var DM_Gia2 = parseFloat(dataQueueDetails.DM_Gia2);
            DM_Gia2 = DM_Gia2.toString() == 'NaN' ? '-' : DM_Gia2;
            var DM_Gia3 = parseFloat(dataQueueDetails.DM_Gia3);
            DM_Gia3 = DM_Gia3.toString() == 'NaN' ? '-' : DM_Gia3;
            var DB_KL1 = parseInt(dataQueueDetails.DB_KL1);
            var DB_KL2 = parseInt(dataQueueDetails.DB_KL2);
            var DB_KL3 = parseInt(dataQueueDetails.DB_KL3);
            var DB_Gia1 = parseFloat(dataQueueDetails.DB_Gia1);
            DB_Gia1 = DB_Gia1.toString() == 'NaN' ? '-' : DB_Gia1;
            var DB_Gia2 = parseFloat(dataQueueDetails.DB_Gia2);
            DB_Gia2 = DB_Gia2.toString() == 'NaN' ? '-' : DB_Gia2;
            var DB_Gia3 = parseFloat(dataQueueDetails.DB_Gia3);
            DB_Gia3 = DB_Gia3.toString() == 'NaN' ? '-' : DB_Gia3;
            var totalBuyVolume = 0;
            totalBuyVolume += DM_KL1.toString() == "NaN" ? 0 : DM_KL1;
            totalBuyVolume += DM_KL2.toString() == "NaN" ? 0 : DM_KL2;
            totalBuyVolume += DM_KL3.toString() == "NaN" ? 0 : DM_KL3;
            var totalSellVolume = 0;
            totalSellVolume += DB_KL1.toString() == "NaN" ? 0 : DB_KL1;
            totalSellVolume += DB_KL2.toString() == "NaN" ? 0 : DB_KL2;
            totalSellVolume += DB_KL3.toString() == "NaN" ? 0 : DB_KL3;

            var htmlCurrent = htmlTemplate.replace('DM_KL1', DinhDang(DM_KL1)).replace('DM_KL2', DinhDang(DM_KL2)).replace('DM_KL3', DinhDang(DM_KL3)).replace('DB_KL1', DinhDang(DB_KL1)).replace('DB_KL2', DinhDang(DB_KL2)).replace('DB_KL3', DinhDang(DB_KL3));
            htmlCurrent = htmlCurrent.replace('DM_Gia1', DM_Gia1).replace('DM_Gia2', DM_Gia2).replace('DM_Gia3', DM_Gia3).replace('DB_Gia1', DB_Gia1).replace('DB_Gia2', DB_Gia2).replace('DB_Gia3', DB_Gia3);
            htmlCurrent = htmlCurrent.replace('DateData', DataDate).replace('ToTalSellVolume', DinhDang(totalSellVolume)).replace('TotalBuyVolume', DinhDang(totalBuyVolume));
            htmlCurrent = htmlCurrent.replace('DM_percent1', GetPercentProgress(DM_KL1, MaxQueue)).replace('DM_percent2', GetPercentProgress(DM_KL2, MaxQueue)).replace('DM_percent3', GetPercentProgress(DM_KL3, MaxQueue));
            htmlCurrent = htmlCurrent.replace('DB_percent1', GetPercentProgress(DB_KL1, MaxQueue)).replace('DB_percent2', GetPercentProgress(DB_KL2, MaxQueue)).replace('DB_percent3', GetPercentProgress(DB_KL3, MaxQueue));
            //set color cho gia
            htmlCurrent = htmlCurrent.replace('classDMGia1', SetColor(DM_Gia1, GiaThamChieu)).replace('classDMGia2', SetColor(DM_Gia2, GiaThamChieu)).replace('classDMGia3', SetColor(DM_Gia3, GiaThamChieu));
            htmlCurrent = htmlCurrent.replace('classDBGia1', SetColor(DB_Gia1, GiaThamChieu)).replace('classDBGia2', SetColor(DB_Gia2, GiaThamChieu)).replace('classDBGia3', SetColor(DB_Gia3, GiaThamChieu));
            if (i == 2) {
                htmlCurrent = htmlCurrent.replace('row_margin_left', '25%');
            }
            else
                htmlCurrent = htmlCurrent.replace('row_margin_left', '0');


            htmlCurrent += '</div></div>';
            html += htmlCurrent;
        }
        DrawMatched(data);
        $('.modalHistoryData .modal-body .left-modal').html(html);
    }

    function DrawTableTaiChinh(stock) {
        $.ajax({
            url: "/Resources/GetFinance.ashx",
            method: "POST",
            data: { stock: stock },
            dataType: "json",
            success: function (response) {
                if (response.Status == 1) {
                    var data = response.Data;
                    var html = '<table class="table table-bordered table-hover tblc">';
                    html += '                                <thead>';
                    html += '                                      <tr>';
                    html += '                                          <td><%= DGSFramework.Globals.GetLanguage("Giai đoạn", "Period", true)%></td>';
                    html += '                                          <td>ROE(%)</td>';
                    html += '                                          <td>ROA(%)</td>';
                    html += '                                          <td>Revenue</td>';
                    html += '                                          <td>NP</td>';
                    html += '                                          <td>EPS</td>';
                    html += '                                          <td>PE</td>';
                    html += '                                          <td>PB</td>';
                    html += '                                          <td>QR</td>';
                    html += '                                          <td>NAPS</td>';
                    html += '                                      </tr>';
                    html += '                                  </thead>';
                    data.forEach(function (item) {
                        var naps = null;
                        if (item.TotalAssets != null && item.TotalLiabilities && item.KLLHBinhQuan != null && item.KLLHBinhQuan != 0) {
                            naps = (item.TotalAssets - item.TotalLiabilities) / item.KLLHBinhQuan;
                        }
                        html += '<tr>';
                        if (item.Quarter == null)
                            html += '<td>' + item.Year + '</td>';
                        else
                            html += '<td>' + item.Year + '-Q' + item.Quarter + '</td>';
                        html += '<td>' + (item.ROE == null ? '-' : addPeriod(item.ROE)) + '</td>';
                        html += '<td>' + (item.ROA == null ? '-' : addPeriod(item.ROA)) + '</td>';
                        html += '<td>' + (item.Revenue == null ? '-' : ConvertShortPrice(item.Revenue)) + '</td>';
                        html += '<td>' + (item.NP == null ? '-' : ConvertShortPrice(item.NP)) + '</td>';
                        html += '<td>' + (item.EPS == null ? '-' : addPeriod(item.EPS)) + '</td>';
                        html += '<td>' + (item.PE == null ? '-' : addPeriod(item.PE)) + '</td>';
                        html += '<td>' + (item.PB == null ? '-' : addPeriod(item.PB)) + '</td>';
                        html += '<td>' + (item.QR == null ? '-' : addPeriod(item.QR)) + '</td>';
                        html += '<td>' + (naps != null ? ConvertShortPrice(naps) : '-') + '</td>';
                        html += '</tr>';
                    })
                    html += '</table>';
                    $('#tblTKTaiChinh').html(html);
                }
            }
        })


    }

    function DrawChart(data) {
        //var html = '<img src="Images/s.jpg" style="width: 100%; height: 225px;">';
        //$('.detail-chart').html(html);
    }

    function GetPercentProgress(value, maxValue) {
        var returnValue = 0;
        if (value > 0) {
            returnValue = value / maxValue * 100;
        }
        return returnValue;
    }

    function SetColorStockList() {
        var lst = $('#stockList');
        $('#stockList tr').each(function (i, itemi) {
            if (i != 0) {
                var currentArray = $(itemi).find('td').eq(1);
                var thamChieuValue = parseFloat(currentArray.text());
                $(itemi).find('td').each(function (j, itemj) {
                    if (j >= 1 && j <= 6) {
                        var currentValue = parseFloat($(itemj).text());
                        $(itemj).addClass(SetColor(currentValue, thamChieuValue));
                    }
                })
            }
        });
    }

    function SetColor(currentValue, thamChieuValue) {
        var strReturn = '';
        if (currentValue > thamChieuValue)
            strReturn = 'color_green';
        else if (currentValue < thamChieuValue)
            strReturn = 'color_red';
        //else if (currentValue == thamChieuValue)
        //    strReturn = 'color_yellow';
        return strReturn;
    }

    var pagingCurrrentPage = 1, pagingTotalPage = 0, typeStock = 'allstock';

    function DinhDang(giaTri) {

        var kyHieuTachNhom = ',', kyHieuThapPhan = '.', soChuSoThapPhan = 2;

        giaTri != null ? giaTri.toString().replace(/\$|\,/g, '') : giaTri;

        //GIÁ TRỊ KHÔNG PHẢI LÀ SỐ (KHÔNG HỢP LỆ) CHUYỂN VỀ 0
        if (isNaN(giaTri)) giaTri = "0";

        var j = 1;
        for (var i = 0; i < soChuSoThapPhan; i++) {
            j += "0";
        }

        //LÀM TRÒN GIÁ TRỊ PHẦN THẬP PHÂN
        giaTri = Math.floor(giaTri * j + 0.50000000001);

        phanThapPhan = giaTri % j;

        giaTri = Math.floor(giaTri / j).toString();

        if (phanThapPhan < 10) phanThapPhan = "0" + phanThapPhan;

        for (var i = 0; i < Math.floor((giaTri.length - (1 + i)) / 3) ; i++)
            giaTri = giaTri.substring(0, giaTri.length - (4 * i + 3)) + kyHieuTachNhom + giaTri.substring(giaTri.length - (4 * i + 3));
        if (giaTri.indexOf('-,') >= 0)
            giaTri = giaTri.replace('-,', '-');

        if (giaTri != '0') {
            if (soChuSoThapPhan) {
                if (phanThapPhan != "00") {
                    return giaTri + kyHieuThapPhan + phanThapPhan;
                }
                else return giaTri;
            }
            else
                return giaTri;
        }
        else { return '0'; }
    }
      

    function LoadChart(ticket, currentDate) {
        //clear chart
        $('#chart').html('');
        $('#lineTooltip').hide();
        $.ajax({
            url: "/Resources/GetStockInfo.ashx",
            method: "POST",
            data: { stock: ticket, gettime: currentDate },
            dataType: "json",
            success: function (response) {
                var data = response;
                if (data == null) {
                    plot = window.tickp("#chart");
                    plot.plotempty();
                    alert('Không có mã chứng khoán này.');
                }
                else if (data.length == 0) {
                    plot = window.tickp("#chart");
                    plot.plotempty();
                }
                else if (data != undefined) {
                    dataStock = data;
                    dataTemp = data;
                    plot = window.tickp("#chart"); // delete it has draw null
                    plot.read(ticker, data);
                    plot.cp.type = 1;
                    plot.cp.zt = 3;
                    plot.width = 200;
                    plot.zoomtype(3, dataStock); // modified huynq 25-07-2014  plot.plot();           
                    plot.addindicator('sma', ["30"]);
                    plot.addindicator('macd', ["12", "26", "9"]);
                    plot.addindicator('momentum', ["14"]); //thay bang stochrsi
                    plot.addindicator('rsi', ["14"]);
                    plot.addindicator('stoch', ["14", "14", "3"]);
                    //set default button
                    $('#chartoneyear').addClass('active');
                    $('#chartfouryear').removeClass('active');
                    $('#chartsixmonth').removeClass('active');
                }
                else {
                    dataStock = dataTemp;
                    plot = window.tickp("#chart"); // delete it has draw null
                    plot.zoomtype(3, dataStock);
                    alert('Không có mã chứng khoán này.');
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });

        //if (data == null) {
        //    plot = window.tickp("#chart");
        //    plot.plotempty();
        //    alert('Không có mã chứng khoán này.');
        //}
        //else if (data.length == 0) {
        //    plot = window.tickp("#chart");
        //    plot.plotempty();
        //}
        //else if (data != undefined) {
        //    dataStock = data;
        //    dataTemp = data;
        //    plot = window.tickp("#chart"); // delete it has draw null
        //    plot.read(ticker, data);          
        //    plot.cp.type = 5;
        //    plot.width = 200;
        //    plot.zoomtype(2, dataStock); // modified huynq 25-07-2014  plot.plot();           
        //    plot.addindicator('ema', ["20"]);
        //    plot.addindicator('stoch', ["14", "14", "3"]);
        //    plot.addindicator('macd', ["12", "26", "9"]);
        //    plot.addindicator('rsi', ["14"]);

        //}
        //else {
        //    dataStock = dataTemp;
        //    plot = window.tickp("#chart"); // delete it has draw null
        //    plot.zoomtype(2, dataStock);
        //    alert('Không có mã chứng khoán này.');
        //}
    }

    function GetHoseIndex() {
        $.ajax({
            url: "/Resources/GetHoseIndex.ashx",
            method: "POST",
            dataType: "json",
            success: function (response) {
                if (response.Status === 1) {
                    var data = response.Data;
                    var VN0Price = data.VN0Price, VN0Up = data.VN0Up, VN0UpPercent = data.VN0UpPercent, VN0Quantity = data.VN0Quantity, VN0Value = data.VN0Value;
                    var VN3Price = data.VN3Price, VN3Up = data.VN3Up, VN3UpPercent = data.VN3UpPercent, VN3Quantity = data.VN3Quantity, VN3Value = data.VN3Value;
                    var HN0Price = data.HN0Price, HN0Up = data.HN0Up, HN0UpPercent = data.HN0UpPercent, HN0Quantity = data.HN0Quantity, HN0Value = data.HN0Value;
                    var HN3Price = data.HN3Price, HN3Up = data.HN3Up, HN3UpPercent = data.HN3UpPercent, HN3Quantity = data.HN3Quantity, HN3Value = data.HN3Value;
                    var ColorVN0 = VN0Up > 0 ? 'vni_green' : 'vni_red';
                    var ColorVN3 = VN3Up > 0 ? 'vni_green' : 'vni_red';
                    var ColorHN0 = HN0Up > 0 ? 'vni_green' : 'vni_red';
                    var ColorHN3 = HN3Up > 0 ? 'vni_green' : 'vni_red';
                    var VN0Html = DinhDang(VN0Price) + ' (<i class="' + ColorVN0 + '">' + VN0Up + '</i>,<i class="' + ColorVN0 + '"> ' + VN0UpPercent + '%</i>)';
                    var VN3Html = DinhDang(VN3Price) + ' (<i class="' + ColorVN3 + '">' + VN3Up + '</i>,<i class="' + ColorVN3 + '"> ' + VN3UpPercent + '%</i>)';
                    var HN0Html = DinhDang(HN0Price) + ' (<i class="' + ColorHN0 + '">' + HN0Up + '</i>,<i class="' + ColorHN0 + '"> ' + HN0UpPercent + '%</i>)';
                    var HN3Html = DinhDang(HN3Price) + ' (<i class="' + ColorHN3 + '">' + HN3Up + '</i>,<i class="' + ColorHN3 + '"> ' + HN3UpPercent + '%</i>)';
                    $('#spVNI').html(VN0Html);
                    $('#spVN30').html(VN3Html);
                    $('#spHNX').html(HN0Html);
                    $('#HNX30').html(HN3Html);
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        })
    }

    function Loading() {
        //var wHeight = $(window).height();
        //var html = '<div id="loading" style="height:' + wHeight + 'px"></div>';
        //$('body').append(html);
    }
    //function ClearLoading() {
    //    var loadingTag = $('div#loading');
    //    loadingTag.remove();
    //}

    //dung de check bt,st
    function DrawTableCheck(data) {        
        if ($('.modalHistoryTable').length <= 0)
            return;        
        var data = data[0];
        var html = '<table border="1" style="color:#fff;font-size:11px;width:100%"><tr><td>Time</td><td>DM_Gia3</td><td>DM_Gia2</td><td>DM_Gia1</td><td>KL_Gia</td><td>KL_KL</td><td>DB_Gia1</td><td>DB_Gia2</td><td>DB_Gia3</td></tr>';
        if (data != null) {
            var tblCurrentPriceBoardHtml = '';
            var arrayValue = data.ArrayValue.split('^');
            var arrayQueue = [];
            var arrayQueueKL_Gia = [];
            var preDM = -1;
            var preDB = -1;
            var isFirst = true;
            for (var j = 0; j < arrayValue.length; j++) {
                var dataArray = null;
                if (arrayValue[j] != null)
                    dataArray = arrayValue[j].split('|');
                if (dataArray.length > 10) {
                    var DM_Gia3 = dataArray[2], DM_Gia2 = dataArray[4], DM_Gia1 = dataArray[6], KL_Gia = dataArray[8], KL_KL = dataArray[9];
                    var DB_Gia1 = dataArray[11], DB_Gia2 = dataArray[13], DB_Gia3 = dataArray[15];
                    var date = (dataArray[0].split(' '))[1];
                    var DB = '', DM = '';
                    DB = DB_Gia1;
                    DM = DM_Gia1;
                    //if (DM_Gia1 != null && DM_Gia1 != '')
                    //    DM = DM_Gia1;
                    //else if (DM_Gia2 != null && DM_Gia2 != '')
                    //    DM = DM_Gia2;
                    //else if (DM_Gia3 != null && DM_Gia3 != '')
                    //    DM = DM_Gia3;
                    //if (DB_Gia1 != null && DB_Gia1 != '')
                    //    DB = DB_Gia1;
                    //else if (DB_Gia2 != null && DB_Gia2 != '')
                    //    DB = DB_Gia2;
                    //else if (DB_Gia3 != null && DB_Gia3 != '')
                    //    DB = DB_Gia3;
                    if (DB == 'ATC' || DB == 'ATO' || DM == 'ATC' || DM == 'ATO') {

                        html += '<tr>'
                        html += '<td class="color_purple">' + date + '</td><td>' + DM_Gia3 + '</td>' + '<td>' + DM_Gia2 + '</td>' + '<td>' + DM_Gia1 + '</td>' + '<td>' + KL_Gia + '</td>' + '<td>' + KL_KL + '</td>';
                        html += '<td>' + DB_Gia1 + '</td>' + '<td>' + DB_Gia2 + '</td>' + '<td>' + DB_Gia3 + '</td>';
                        html += '</tr>';
                    }
                    else if (KL_Gia != '' && KL_KL != '' && KL_KL.indexOf('+006') < 0) {
                        if (isFirst == false) {
                            if (DM == '')
                                DM = preDM;
                            if (DB == '')
                                DB = preDB;
                        }
                        preDM = DM;
                        preDB = DB;
                        isFirst = false;

                        var color = 'color_red';
                        if (KL_Gia == DB || KL_Gia > DM)
                            color = 'color_green';
                        //var color = '';

                        //if (DB = '') {
                        //    if(DB==KL_Gia)
                        //        color = 'color_green';
                        //    else 
                        //        color = 'color_red';
                        //}
                        //else if (DB == 'color_red')
                        //    color = '';
                        //else
                        //    color = KL_Gia > DM ? 'color_green' : 'color_red';
                        KL_Gia = parseFloat(KL_Gia);
                        DM = parseFloat(DM);
                        DB = parseFloat(DB);
                        html += '<tr>'
                        html += '<td class="' + color + '">' + date + '</td><td>' + DM_Gia3 + '</td>' + '<td>' + DM_Gia2 + '</td>' + '<td>' + DM_Gia1 + '</td>' + '<td>' + KL_Gia + '</td>' + '<td>' + KL_KL + '</td>';
                        html += '<td>' + DB_Gia1 + '</td>' + '<td>' + DB_Gia2 + '</td>' + '<td>' + DB_Gia3 + '</td>';
                        html += '</tr>';
                        KL_Gia = parseFloat(KL_Gia);
                        arrayQueue.push([KL_Gia, DM, DB, KL_KL]);
                        if ($.inArray(KL_Gia, arrayQueueKL_Gia) < 0 && KL_Gia > 0)
                            arrayQueueKL_Gia.push(KL_Gia);
                    }
                    else {
                        //console.log("Date: " + date + " DM_Gia3: " + DM_Gia3 + " DM_Gia2: " + DM_Gia2 + " DM_Gia1: " + DM_Gia1 + " KL_KL: " + KL_Gia + " KL_KL:" + KL_KL + " DB_Gia1: " + DB_Gia1 + " DB_Gia2: " + DB_Gia2 + " DB_Gia3: " + DB_Gia3);
                        html += '<tr>'
                        html += '<td>' + date + '</td><td>' + DM_Gia3 + '</td>' + '<td>' + DM_Gia2 + '</td>' + '<td>' + DM_Gia1 + '</td>' + '<td>' + KL_Gia + '</td>' + '<td>' + KL_KL + '</td>';
                        html += '<td>' + DB_Gia1 + '</td>' + '<td>' + DB_Gia2 + '</td>' + '<td>' + DB_Gia3 + '</td>';
                        html += '</tr>';
                    }
                }
            }
        }
        $('.modalHistoryTable .modal-body').html(html + '</table>');
    }
    function showServerTime(obj, time) {

        var parts = time.split(":"),
            newTime = new Date(),
            timeDifference = new Date().getTime() - newTime.getTime();

        newTime.setHours(parseInt(parts[0], 10));
        newTime.setMinutes(parseInt(parts[1], 10));
        newTime.setSeconds(parseInt(parts[2], 10));

        var methods = {

            displayTime: function () {
                var now = new Date(new Date().getTime() - timeDifference);
                obj.text([

                    methods.leadZeros(now.getHours(), 2),
                    methods.leadZeros(now.getMinutes(), 2),
                    methods.leadZeros(now.getSeconds(), 2)

                ].join(":"));
                setTimeout(methods.displayTime, 500);
            },
            leadZeros: function (time, width) {

                while (String(time).length < width) {
                    time = "0" + time;
                }
                return time;

            }
        }
        methods.displayTime();
    }
</script>
</html>
